iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 27
0
Modern Web

JS煉金術:Javascript30+聲光玩轉的Drum Pads系列 第 27

[JS30]DAY26: Strip Follow Along Nav

  • 分享至 

  • xImage
  •  

[程式碼&DEMO] [HackMD完整筆記]

目標


隨著滑鼠的移動去展開選單效果。

步驟流程

**STEP1 **

  const nav = document.querySelector('.top');
  const triggers = document.querySelectorAll('.cool > li');
  const background = document.querySelector('dropdownBackground');

  function handleEnter(){
    console.log('Enter!');
  }

  function handleLeave(){
    console.log('Leave!');
  }

  triggers.forEach(trigger => trigger.addEventListener('mouseenter', handleEnter));
  triggers.forEach(trigger => trigger.addEventListener('mouseleave', handleLeave));

**STEP2 **

function handleEnter(){
    // console.log('Enter!');
    this.classList.add('trigger-enter');
    setTimeout(()=>this.classList.add('trigger-enter-active'), 150);
    background.classList.add('open');
  }

  function handleLeave(){
    // console.log('Leave!');
    this.classList.remove('trigger-enter', 'trigger-enter-active');
    background.classList.remove('open');
  }

**STEP3 **

function handleEnter(){
    // console.log('Enter!');
    this.classList.add('trigger-enter');
    setTimeout(()=>this.classList.add('trigger-enter-active'), 150);
    background.classList.add('open');

    const dropdown = this.querySelector('.dropdown');
    const dropdownCoords = dropdown.getBoundingClientRect();

    const coords = {
      height : dropdownCoords.height,
      width : dropdownCoords.width,
      top : dropdownCoords.top,
      left : dropdownCoords.left,
    }

    background.style.setProperty('width', `${coords.width}px`);
    background.style.setProperty('height', `${coords.height}px`);
    background.style.setProperty('transform', `translate(${coords.left}px, ${coords.top}px)`);
  }

上一篇
[JS30]DAY25 : Event Capture, Propagation, Bubbling and Once
下一篇
[JS30] DAY27 : Click And Drag
系列文
JS煉金術:Javascript30+聲光玩轉的Drum Pads30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言